package com.action.app_noverify;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;



import com.enetiy.Areainfo;
import com.enetiy.Areaip;
import com.enetiy.Binding;
import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.operation.HotspotLink;
import com.service.AreaIpService;
import com.service.AreainfoService;
import com.service.BindingService;
import com.service.LoginService;
import com.service.UseraccountService;
import com.service.UserinfoService;
import com.util.DataUtil;
import com.util.Log4gUtil;
import com.util.Md5Util;


public class PCAction {
	private UseraccountService useraccountService;
	private BindingService bindingService;
	private AreainfoService areainfoService;
	private AreaIpService areaIpService;
	private LoginService loginService;
	private UserinfoService userinfoService;
	
	
	private String phone;
	private String account;
	private String userName;
	private String pwd;
	private String NF_callid;
	private String token;
	
	private String params;
	private int userid;
	private int siteAreaid;
	
	
	private void verify(String params){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("phone"))
				phone= dataParser.getString("phone");
			if(dataParser.has("userName")){
				userName= dataParser.getString("userName");
			}
			
			if(dataParser.has("pwd"))
				pwd= dataParser.getString("pwd");
			if(dataParser.has("userid"))
				userid= dataParser.getInt("userid");
			if(dataParser.has("siteAreaid"))
				siteAreaid= dataParser.getInt("siteAreaid");
		}
	}
	
	/**
	 * 设置更新绑定信息  Nf060
	 * 
	 */
	public void setBingdingInfo(){
		verify(params);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		net.sf.json.JSONObject send =DataUtil.sendJson();
		try {
			Log4gUtil.Info("setBingdingInfo-更新绑定", userid+"","进入params", params, NF_callid, token);
			
			out = response.getWriter();
			Areainfo areainfo = areainfoService.selectAreainfo(siteAreaid, "NF-net-00");//绑定权限
			Binding bind = bindingService.selectbinding(phone, siteAreaid);
			Useraccount useraccount = useraccountService.selectUseraccount(phone);
			Areaip areaip = areaIpService.selectAreaip(siteAreaid);
			
			if(areaip!=null&&areainfo!=null&&useraccount!=null){
				Users u = loginService.selectUserId(userid);
				u.setPwd(pwd);
				loginService.updateUser(u);
				if(bind==null){        
					String url = areainfo.getVerifyUrl();
					HotspotLink link = new HotspotLink();
					int hot= link.hotspotVerify(phone, userName, siteAreaid, url, areaip.getPublicIp(), userid, NF_callid, token);
					if(hot==1){
						send.put("code", 1);
						Binding binding = new Binding(areaip.getArea(),useraccount.getUsers(),areainfo,new Date(),phone,userName,1,"");
						bindingService.insertBinding(binding);
					}else{
						send.put("code", 0);
						send.put("msg", link.msg);
					}
				}else{
					send.put("code", 1); 
				}
			}else{
				send.put("code", 0);
				send.put("msg", "数据有误");
			}
		} catch (Exception e) {
			send.put("code", -1);
			send.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(send);
		Log4gUtil.Info("setBingdingInfo-更新绑定", userid+"","输出", send.toString(), NF_callid, token);
	}
	/**
	 * 
	 * 验证绑定
	 */
	public void verifyBinding(){
		if(params!=null&&!params.equals("")){
			org.json.JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("account"))
				account=datajson.getString("account");
			if(datajson.has("pwd"))
				pwd = datajson.getString("pwd");
			if(datajson.has("siteAreaid"))
				siteAreaid = datajson.getInt("siteAreaid");
			}
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		net.sf.json.JSONObject send =DataUtil.sendJson();
		try {
			Log4gUtil.Info("verifyBinding-验证绑定", userid+"","进入params", params, NF_callid, token);
			out = response.getWriter();
			Binding bin= bindingService.selsctbinding(account, siteAreaid, pwd);
			if(bin==null){
				Areainfo areainfo = areainfoService.selectAreainfo(siteAreaid, "NF-net-00");
				
				if(areainfo!=null){
					String url  = areainfo.getVerifyUrl();
					Areaip areaip = areaIpService.selectAreaip(siteAreaid);
					HotspotLink hotspotLink = new HotspotLink();
					int result = hotspotLink.hotspotVerify(account, pwd, siteAreaid, url, areaip.getPublicIp(), userid, NF_callid, token);
					if(result==1){
						send.put("code", 1);
					}
				}else{
					send.put("code", 0);
					send.put("msg", "区域不存在");
				}
			}else{
				send.put("code", 1);
			}
		} catch (Exception e) {
			send.put("code", -1);
			send.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(send);
		Log4gUtil.Info("verifyBinding-验证绑定", userid+"","输出", send.toString(), NF_callid, token);
	}
	/**
	 * 删除绑定 		
	 */
	public void delBingding(){
		verify(params);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		net.sf.json.JSONObject send =DataUtil.sendJson();
		try {
			Log4gUtil.Info("delBingding-删除绑定", userid+"","进入params", params, NF_callid, token);
			out = response.getWriter();
			if(phone!=null&&!phone.equals("")&&siteAreaid!=0){
				Binding binding = bindingService.selectbinding(phone, siteAreaid);
				if(binding!=null){
					Userinfo userinfo = userinfoService.selectUserinfo(binding.getUsers().getId());
					userinfo.setBinding(null);
					userinfoService.updateUserinfo(userinfo);
					bindingService.deleteBingding(binding);
				}	
				send.put("code", 1);
			}else{
				send.put("code", 0);
				send.put("msg", "参数为空");
			}	
		} catch (Exception e) {
			send.put("code", -1);
			send.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(send);
		Log4gUtil.Info("delBingding-删除绑定", userid+"","输出", send.toString(), NF_callid, token);
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}


	public void setNF_callid(String nF_callid) {
		NF_callid = nF_callid;
	}


	public void setToken(String token) {
		this.token = token;
	}


	public void setUserid(int userid) {
		this.userid = userid;
	}


	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public void setSiteAreaid(int siteAreaid) {
		this.siteAreaid = siteAreaid;
	}


	public void setBindingService(BindingService bindingService) {
		this.bindingService = bindingService;
	}


	public void setAreainfoService(AreainfoService areainfoService) {
		this.areainfoService = areainfoService;
	}


	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}


	public void setPwd(String pwd) {
		this.pwd = pwd;
	}


	public void setAccount(String account) {
		this.account = account;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}


	
}
	
